package com.usian.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.feign.BehaviorFeign;
import com.usian.mapper.ApUserSearchMapper;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.search.dto.UserSearchDto;
import com.usian.model.search.pojo.ApUserSearch;
import com.usian.model.user.pojos.ApUser;
import com.usian.service.ApUserSearchService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {

    @Autowired
    private BehaviorFeign behaviorFeign;

    @Override
    public ResponseResult findList(UserSearchDto dto) {
        //1:校验参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2:获取用户的行为实体  feign  entryId --->id
        ApUser user = AppThreadLocalUtils.getUser();
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry behaviorEntry = behaviorFeign.findByUserId(user.getId(), null);
        if(behaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //3:分页查询
        Page page =new Page(dto.getPageNum(),dto.getPageSize());
        IPage result =this.page(page, Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId,behaviorEntry.getId()).eq(ApUserSearch::getStatus,1));

        return ResponseResult.okResult(result.getRecords());
    }

    @Override
    public ResponseResult delSearch(UserSearchDto dto) {
        //1：校验参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2: 根据 entryId  and  当前搜索记录id  status 0
        boolean update = this.update(Wrappers.<ApUserSearch>lambdaUpdate().set(ApUserSearch::getStatus, 0).eq(ApUserSearch::getId, dto.getId()));
        if(update){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    @Async("taskExecutor")
    public void insert(Integer entryId, String searchWords) {

        //查询搜索记录是否存在  去重
        ApUserSearch apUserSearch = this.getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, entryId).eq(ApUserSearch::getKeyword, searchWords));
        if(apUserSearch==null){
            apUserSearch = new ApUserSearch();
            apUserSearch.setEntryId(entryId);
            apUserSearch.setStatus(1);
            apUserSearch.setKeyword(searchWords);
            apUserSearch.setCreatedTime(new Date());
            save(apUserSearch);
        }

    }
}
